********************************************************************************
********************************************************************************
***
*** Filename: 	Set-Up-Data-Maps.do
*** Author: 	David A Bateman
*** Function: 	Converts the delegate-level data on voting on abolition and 
***				Black rights into county-level data to use in maps in 
***				online appendix. 
***	Last update: January 1, 2023
***
********************************************************************************
********************************************************************************

cd "..\Deeper Roots"
set more off

********************************************************************************
*** Virginia
use "Dataverse\Files\Virginia-1831-1832-Roll-Calls.dta", clear
replace district = upper(district)
** The only vote on free Black rights that is liberal in orientation is rc1832b
** the vote to make removal consensual. Flip this vote. 
replace rc1832b  = abs(1- rc1832b)
gen state = 40

replace district = "GREENSVILLE" if district == "GREENVILLE" & state==40
replace district = "NORFOLK CITY" if district == "NORFOLK BOROUGH" & state==40
replace district = "KING AND QUEEN" if district == "KING, QUEEN" & state==40

expand 2 if district=="MASON, JACKSON"
replace district = "MASON" if district=="MASON, JACKSON" & _n==_N
replace district = "JACKSON" if district=="MASON, JACKSON" & _n!=_N

expand 2 if district=="MATTHEWS, MIDDLESEX"
replace district = "MATHEWS" if district=="MATTHEWS, MIDDLESEX" & _n==_N
replace district = "MIDDLESEX" if district=="MATTHEWS, MIDDLESEX" & _n!=_N

expand 2 if district=="LANCASTER, RICHMOND"
replace district = "LANCASTER" if district=="LANCASTER, RICHMOND" & _n==_N
replace district = "RICHMOND" if district=="LANCASTER, RICHMOND" & _n!=_N

expand 2 if district=="FAYETTE, NICHOLAS"
replace district = "FAYETTE" if district=="FAYETTE, NICHOLAS" & _n==_N
replace district = "NICHOLAS" if district=="FAYETTE, NICHOLAS" & _n!=_N

expand 2 if district=="CHARLES CITY, NEW KENT"
replace district = "CHARLES CITY" if district=="CHARLES CITY, NEW KENT" & _n==_N
replace district = "NEW KENT" if district=="CHARLES CITY, NEW KENT" & _n!=_N

expand 2 if district=="ELIZABETH CITY AND WARWICK"
replace district = "ELIZABETH CITY" if district=="ELIZABETH CITY AND WARWICK" & _n==_N
replace district = "WARWICK" if district=="ELIZABETH CITY AND WARWICK" & _n!=_N
expand 2 if district=="ELIZABETH CITY, WARWICK"
replace district = "ELIZABETH CITY" if district=="ELIZABETH CITY, WARWICK" & _n==_N
replace district = "WARWICK" if district=="ELIZABETH CITY, WARWICK" & _n!=_N

expand 3 if district=="JAMES CITY, YORK, WILLIAMSBURG" 
bys district delegate: replace district = "JAMES CITY" if district=="JAMES CITY, YORK, WILLIAMSBURG" & delegate=="Semple" & _n==_N
bys district delegate: replace district = "YORK" if district=="JAMES CITY, YORK, WILLIAMSBURG" & delegate=="Semple" & _n==_N-1
bys district delegate: replace district = "WILLIAMSBURG" if district=="JAMES CITY, YORK, WILLIAMSBURG" & delegate=="Semple" 
bys district delegate: replace district = "JAMES CITY" if district=="JAMES CITY, YORK, WILLIAMSBURG" & delegate=="SHEILD" & _n==_N
bys district delegate: replace district = "YORK" if district=="JAMES CITY, YORK, WILLIAMSBURG" & delegate=="SHEILD" & _n==_N-1
bys district delegate: replace district = "WILLIAMSBURG" if district=="JAMES CITY, YORK, WILLIAMSBURG" & delegate=="SHEILD" 


collapse rc1831a rc1831b rc1832a rc1832b rc1832c rc1832d  , by(district state)
egen new = rowmean(rc1831a rc1831b rc1832b rc1832c rc1832d)

preserve
	rename rc1832a abolition
	keep district new state abolition
	tempfile saveVA
	save `saveVA',replace
restore

/*
preserve
	rename rc1832a abolition
	keep district abolition state
	tempfile saveVA2
	save `saveVA2',replace
restore
*/
*******************************************************
use "Dataverse\Files\NC-TN-1834-1835-Roll-Calls.dta", clear
replace county = upper(county)
rename county district
gen state = 47 if state_id==2
replace state = 54 if state_id==1
drop state_id


**TN
replace district = "EDGECOMBE" if district == "EDGECOMB" & state==47
replace district = "PERQUIMANS" if district == "PERQUIMONS" & state==47
replace district = "YANCEY" if district == "YANCY" & state==47

expand 2 if district=="WEAKLEY, OBION"
replace district = "OBION" if district=="WEAKLEY, OBION" & _n==_N
replace district = "WEAKLEY" if district=="WEAKLEY, OBION" & _n!=_N

expand 2 if district=="RHEA, HAMILTON"
replace district = "RHEA" if district=="RHEA, HAMILTON" & _n==_N
replace district = "HAMILTON" if district=="RHEA, HAMILTON" & _n!=_N

expand 2 if district=="ANDERSON, MORGAN"
replace district = "ANDERSON" if district=="ANDERSON, MORGAN" & _n==_N
replace district = "MORGAN" if district=="ANDERSON, MORGAN" & _n!=_N

expand 2 if district=="OVERTON, FENTRESS"
replace district = "OVERTON" if district=="OVERTON, FENTRESS" & _n==_N
replace district = "FENTRESS" if district=="OVERTON, FENTRESS" & _n!=_N

expand 2 if district=="GIBSON, DYER"
replace district = "GIBSON" if district=="GIBSON, DYER" & _n==_N
replace district = "DYER" if district=="GIBSON, DYER" & _n!=_N

expand 2 if district=="LINCOLN, GILES"
replace district = "LINCOLN" if district=="LINCOLN, GILES" & _n==_N
replace district = "GILES" if district=="LINCOLN, GILES" & _n!=_N

expand 2 if district=="HAYWOOD, TIPTON"
replace district = "HAYWOOD" if district=="HAYWOOD, TIPTON" & _n==_N
replace district = "TIPTON" if district=="HAYWOOD, TIPTON" & _n!=_N

expand 2 if district=="COCKE, SEVIER"
replace district = "COCKE" if district=="COCKE, SEVIER" & _n==_N
replace district = "SEVIER" if district=="COCKE, SEVIER" & _n!=_N

expand 2 if district=="SMITH, SUMNER"
replace district = "SMITH" if district=="SMITH, SUMNER" & _n==_N
replace district = "SUMNER" if district=="SMITH, SUMNER" & _n!=_N

expand 2 if district=="WARREN, FRANKLIN"
replace district = "WARREN" if district=="WARREN, FRANKLIN" & _n==_N
replace district = "FRANKLIN" if district=="WARREN, FRANKLIN" & _n!=_N

expand 3 if district=="PERRY, HARDIN, MCNAIRY" 
bys district delegate: replace district = "PERRY" if district=="PERRY, HARDIN, MCNAIRY" & delegate=="CROSS, M" & _n==_N
bys district delegate: replace district = "HARDIN" if district=="PERRY, HARDIN, MCNAIRY" & delegate=="CROSS, M" & _n==_N-1
bys district delegate: replace district = "MCNAIRY" if district=="PERRY, HARDIN, MCNAIRY" & delegate=="CROSS, M" 
bys district delegate: replace district = "PERRY" if district=="PERRY, HARDIN, MCNAIRY" & delegate=="SCOTT, J" & _n==_N
bys district delegate: replace district = "HARDIN" if district=="PERRY, HARDIN, MCNAIRY" & delegate=="SCOTT, J" & _n==_N-1
bys district delegate: replace district = "MCNAIRY" if district=="PERRY, HARDIN, MCNAIRY" & delegate=="SCOTT, J" 

expand 3 if district=="DICKSON, STEWART, HUMPHREYS" 
bys district delegate: replace district = "DICKSON" if district=="DICKSON, STEWART, HUMPHREYS" & delegate=="GRAY, J" & _n==_N
bys district delegate: replace district = "STEWART" if district=="DICKSON, STEWART, HUMPHREYS" & delegate=="GRAY, J" & _n==_N-1
bys district delegate: replace district = "HUMPHREYS" if district=="DICKSON, STEWART, HUMPHREYS" & delegate=="GRAY, J" 
bys district delegate: replace district = "DICKSON" if district=="DICKSON, STEWART, HUMPHREYS" & delegate=="MONTGOMERY, J" & _n==_N
bys district delegate: replace district = "STEWART" if district=="DICKSON, STEWART, HUMPHREYS" & delegate=="MONTGOMERY, J" & _n==_N-1
bys district delegate: replace district = "HUMPHREYS" if district=="DICKSON, STEWART, HUMPHREYS" & delegate=="MONTGOMERY, J" 

expand 3 if district=="HICKMAN, WAYNE, LAWRENCE" 
bys district delegate: replace district = "HICKMAN" if district=="HICKMAN, WAYNE, LAWRENCE" & delegate=="GORDON, B" & _n==_N
bys district delegate: replace district = "WAYNE" if district=="HICKMAN, WAYNE, LAWRENCE" & delegate=="GORDON, B" & _n==_N-1
bys district delegate: replace district = "LAWRENCE" if district=="HICKMAN, WAYNE, LAWRENCE" & delegate=="GORDON, B" 
bys district delegate: replace district = "HICKMAN" if district=="HICKMAN, WAYNE, LAWRENCE" & delegate=="SHARP, H" & _n==_N
bys district delegate: replace district = "WAYNE" if district=="HICKMAN, WAYNE, LAWRENCE" & delegate=="SHARP, H" & _n==_N-1
bys district delegate: replace district = "LAWRENCE" if district=="HICKMAN, WAYNE, LAWRENCE" & delegate=="SHARP, H" 

expand 4 if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" 
bys district delegate: replace district = "JEFFERSON" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="BRADSHAW, R" & _n==_N
bys district delegate: replace district = "GRAINGER" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="BRADSHAW, R" & _n==_N-1
bys district delegate: replace district = "CLAIBORNE" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="BRADSHAW, R" & _n==_N-1
bys district delegate: replace district = "CAMPBELL" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="BRADSHAW, R" 

bys district delegate: replace district = "JEFFERSON" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="GARRETT, G" & _n==_N
bys district delegate: replace district = "GRAINGER" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="GARRETT, G" & _n==_N-1
bys district delegate: replace district = "CLAIBORNE" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="GARRETT, G" & _n==_N-1
bys district delegate: replace district = "CAMPBELL" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="GARRETT, G" 

bys district delegate: replace district = "JEFFERSON" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="HODGES, C" & _n==_N
bys district delegate: replace district = "GRAINGER" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="HODGES, C" & _n==_N-1
bys district delegate: replace district = "CLAIBORNE" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="HODGES, C" & _n==_N-1
bys district delegate: replace district = "CAMPBELL" if district=="JEFFERSON, GRAINGER, CLAIBORNE, CAMPBELL" & delegate=="HODGES, C" 

expand 6 if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" 
bys district delegate: replace district = "WASHINGTON" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" & _n==_N
bys district delegate: replace district = "GREENE" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" & _n==_N-1
bys district delegate: replace district = "SEVIER" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" & _n==_N-1
bys district delegate: replace district = "COCKE" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" & _n==_N-1
bys district delegate: replace district = "BLOUNT" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" & _n==_N-1
bys district delegate: replace district = "MONROE" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" & _n==_N-1
bys district delegate: replace district = "MCMINN" if district=="WASHINGTON, GREENE, SEVIER, COCKE, BLOUNT, MONROE, MCMINN" & delegate=="M'GAUGHEY, J" 


collapse bsuff AntiAbolition, by(district state)

preserve
	egen new = rowmean(bsuff AntiAbolition)
	*** Make the vote about abolition rather than anti-abolition
	replace AntiAbolition  = abs(1- AntiAbolition)
	rename AntiAbolition abolition
	keep district abolition new state
	tempfile saveTNNC
	save `saveTNNC',replace
restore

********************************************************************************
use "Dataverse\Files\MS-1831-Roll-Calls.dta", clear
replace vote2 = abs(1- vote2)
replace vote4 = abs(1- vote4)

replace county = upper(county)
rename county district

collapse vote2 vote4 vote5, by(district fips)
gen state=46
egen new = rowmean( vote2 vote4 vote5)
keep district new state fips

append using `saveVA'
append using `saveTNNC'

rename district name

tempfile saveAll
save `saveAll', replace

use "Dataverse\Census\Census1850.dta", clear
keep if state==40 | state==47 | state==54 | state==46
drop if county==0
keep state name fips
merge 1:1 state name using `saveAll', nogen

tempfile saveAll1
save `saveAll1', replace
use "Dataverse\Census\Census1830.dta", clear
keep if state==40 | state==47 | state==54 | state==46
drop if county==0
keep state name fips
merge 1:1 state name using `saveAll1', nogen

sort  fips state
replace fips = 51830 if fips==. & name =="WILLIAMSBURG"
drop if name=="GREENVILLE"

preserve
	keep state name fips new
	outsheet using "Dataverse\Files\County-Votes-Black-Rights.csv", comma replace
restore

preserve
	gen anyabolition = 1 if abolition>0 & abolition!=.
	replace anyabolition = 0 if abolition == 0
	drop if state == 46 | state ==47
	keep state abolition fips new
	outsheet using "Dataverse\Files\Virginia-TN-Abolition.csv", comma replace
restore
